# 3.12 (Direction Recognition) Geomagnetic Sensor ## 3.12.1 Overview AK8975C geomagnetic sensor is a three-axis electronic compass IC with high sensitivity. It can output 13-bit data and accurately detect X, Y, Z axes geomagnetic values. Thus, it is suitable for portable devices with navigation function such as mobile phones and tablets. ## 3.12.2 Schematic Diagram ![6-12](./media/6-12-2.png) The AK8975C geomagnetic sensor works in the principle of electromagnetic induction. It takes the Earth's magnetic field as a measurement benchmark to sense changes in the magnetic field through its internal magnetic material and coils. Specifically, when the magnetic material is affected by geomagnetic field, a directional constrained electron spin deflection will happen due to the field force, which in turn forms a magnetic field. This field induces potential signals in the coil. This sensor amplifies and processes the induced potential signals, which are then transmitted to the system for further calculation, analysis and processing. So it measures geomagnetic magnetic field in the axis X, Y, and Z to determine the direction. ## 3.12.3 Code Blocks 1. ![6-12](./media/j39.png) This block is used to initialize AK8975 geomagnetic sensor. 2. ![j40](./media/j40.png) This block reads the magnetic field direction. 3. ![j41](./media/j41.png) This block reads the values on axis X, Y and Z: ![6-12](./media/j42.png) ## 3.12.4 Test Code ### 3.12.4.1 Build Code There are two ways to upload the code: directly open the code file we provide; or manually build blocks. **Directly open the code file we provide:** 1. Click ![](./media/j68.png) and choose `Load from your computer` ![](./media/j67.png) 2. We have already downloaded the codes on computer desktop, so open the file and choose `3-12-ak8975.sb3` **Manually build blocks:** 1. In ![events](./media/events.png), find ![j1](./media/j1.png) block. 2. In ![6-12](./media/compas.png), put ![j35](./media/j39.png) under ![j1](./media/j1.png) 3. In ![serial](./media/serial.png), drag ![j12](./media/j12.png) and set baud rate to 9600, and put it under ![j35](./media/j39.png) 4. In ![control](./media/control.png), drag ![j2](./media/j2.png).![6-4-4-1-1](./media/6-2-4-1-1.png) ![6-2](./media/6-2-4-1-2.png) 4. In ![serial](./media/serial.png), put ![j13](./media/j13.png) in ![](./media/j2.png) and set to print “X:” in no-warp mode. 5. Add a ![image-20240701141959486](./media/6-9-4-1-1.png) and set to no-warp mode. 6. In ![](./media/compas.png), put ![](./media/j41.png) into the printing content of ![j13](./media/6-9-4-1-1.png), and set to read axis “X”. ![6-12](./media/6-12-4-1-1.png) 7. Duplicate ![6-12](./media/6-12-4-1-2.png) and change to read “ Y:”(the two spaces separates the two values to be output). Set to read value in axis “Y”. 8. Duplicate ![6-12](./media/6-12-4-1-3.png)and change to read “ Z:”(the spaces separates the two values to be output). Set to read value in axis “Z”. 9. Duplicate ![6-12](./media/6-12-4-1-4.png)and change to read “ Direction:”(the spaces separates the two values to be output). 10. In ![](./media/compas.png), put ![](./media/j40.png) into the printing content of ![j13](./media/6-9-4-1-1.png) and set mode to warp. 11. Add a delay of 1S for better observing the results. **Complete Test Code** ![6-12-4-1-5](./media/6-12-4-1-5.png) ### 3.12.4.2 Test Result After uploading code, the serial monitor prints the sensor values. Move the coding box to see the changes of these values. ![6-12](./media/6-12-4-2.png)